package com.gxuwz.leave.dao;


import com.gxuwz.leave.entity.Class;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ClassDaoImpl implements ClassDao {
    private JdbcTemplate jdbcTemplate;
    @Override
    public void insert(Class entity) {
        String sql = "insert into sys_classes(classID, className, depID, major, grade) values (?, ?, ?, ?, ?)";
        List<Object> paramList = new ArrayList<>();
        if(entity==null){
            return;
        }else{
            paramList.add(entity.getClassID());
            paramList.add(entity.getClassName());
            paramList.add(entity.getDepID());
            paramList.add(entity.getMajor());
            paramList.add(entity.getGrade());
        }
        jdbcTemplate.update(sql, paramList.toArray());
    }

    @Override
    public void delete(String classID) {
        String sql = "delete from sys_classes where classID = ? ";
        if(!"".equals(classID)){
            jdbcTemplate.update(sql, classID);
        }
    }

    @Override
    public void update(Class entity) {
        String sql = "update sys_classes set ";
        List<Object> paramList = new ArrayList<>();
        if(entity ==null){
            return;
        }else{
            if(entity.getClassName()!=null && !"".equals(entity.getClassName())){
                sql+="className = ?, ";
                paramList.add(entity.getClassName());
            }
            if(entity.getDepID()!=null && !"".equals(entity.getDepID())){
                sql+="depID = ?, ";
                paramList.add(entity.getDepID());
            }
            if(entity.getMajor()!=null && !"".equals(entity.getMajor())){
                sql+="major = ?, ";
                paramList.add(entity.getMajor());
            }
            if(entity.getGrade()!=null && !"".equals(entity.getGrade())){
                sql+="grade = ?, ";
                paramList.add(entity.getGrade());
            }
            // 移除多余的逗号和空格
            sql = sql.substring(0, sql.length() - 2);
            sql+="where classID = ?";
            paramList.add(entity.getClassID());
        }
        jdbcTemplate.update(sql, paramList.toArray());
    }

    @Override
    public List<Class> query(Class entity) {
        String sql = "select * from sys_classes where 1=1 ";
        List<Class> classes = new ArrayList<>();
        List<Object> paramList = new ArrayList<>();
        if(entity!=null){
            if(entity.getClassID()!=null && !"".equals(entity.getClassID())){
                sql+="and classID = ?";
                paramList.add(entity.getClassID());
            }
            if(entity.getClassName()!=null && !"".equals(entity.getClassName())){
                sql+="and className like ?";
                paramList.add("%" + entity.getClassName() + "%");
            }
        }
        classes = jdbcTemplate.query(sql, new RowMapper<Class>() {
            @Override
            public Class mapRow(ResultSet resultSet, int i) throws SQLException {
                Class clas = new Class();
                clas.setClassID(resultSet.getString("classID"));
                clas.setClassName(resultSet.getString("className"));
                clas.setDepID(resultSet.getString("depID"));
                clas.setMajor(resultSet.getString("major"));
                clas.setGrade(resultSet.getString("grade"));
                return clas;
            }
        }, paramList.toArray());
        return classes;
    }

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate){
        this.jdbcTemplate = jdbcTemplate;
    }
}
